package net.edu_soft.ec.asset.service.impl;

import com.alibaba.fastjson.JSONObject;
import net.edu_soft.ec.asset.mapper.ProExtensionMapper;
import net.edu_soft.ec.asset.pojo.Label;
import net.edu_soft.ec.asset.pojo.ProExtension;
import net.edu_soft.ec.asset.pojo.ProExtensionExample;
import net.edu_soft.ec.asset.service.ProExtensionService;
import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProExtensionServiceImpl implements ProExtensionService {

    @Autowired
    private ProExtensionMapper proExtensionMapper;


    @Override
    public ProExtension getProExtensionByProductId(int productId) {
        return selByProductId(productId);
    }

    @Override
    public int saveProExtension(ProExtension proExtension) {
        //ProExtension proExtensionByProductId = getProExtensionByProductId(proExtension.getProductId());
        //if(proExtensionByProductId==null){
           // throw new OperationException("该扩展id查询不到扩展信息");
        //}
        PuingUtil.notNullByZero(proExtension.getProductId(),"产品扩展业务：产品id不能为空或小于等于0");
        return proExtensionMapper.insertSelective(proExtension);
    }

    @Override
    public int updateProExtension(ProExtension proExtension) {
        //查询是否存在
        selProExtensionById(proExtension.getProExtensionId());
        //校验产品id
        PuingUtil.notNullByZero(proExtension.getProductId(),"产品扩展业务：产品id不能为空或小于等于0");
        return  proExtensionMapper.updateByPrimaryKeySelective(proExtension);
    }

    @Override
    public List<Label> findLabelById(Integer productId) {
        ProExtension proExtension = selByProductId(productId);
        if(proExtension==null){
            throw new OperationException("产品扩展业务：该产品id查询不到扩展信息");
        }
        //提取
        String labelStr = proExtension.getLabel();
        if(labelStr==null || "".equals(labelStr)){
            return null;
        }
        //转换
        List<Label> labels = JSONObject.parseArray(labelStr, Label.class);
        return labels;
    }


    /***
     * 根据产品扩展id查询产品扩展信息
     * @param proExtensionId id
     * @return 产品扩展
     */
    private ProExtension selProExtensionById(Integer proExtensionId){
        PuingUtil.notNullByZero(proExtensionId,"产品扩展业务：产品扩展id不能为空或小于等于0");
        ProExtension proExtension = proExtensionMapper.selectByPrimaryKey(proExtensionId);
        if(proExtension==null){
            throw new OperationException("该扩展id查询不到扩展信息");
        }
        return proExtension;
    }

    /***
     * 根据产品id查询产品扩展信息
     * @param productId
     * @return
     */
    private ProExtension selByProductId(Integer productId){
        PuingUtil.notNullByZero(productId,"产品扩展业务：产品id不能为空或小于等于0");
        ProExtensionExample example = new  ProExtensionExample();
        ProExtensionExample.Criteria criteria = example.createCriteria();
        //添加产品id
        criteria.andProductIdEqualTo(productId);
        List<ProExtension> extensionList = proExtensionMapper.selectByExample(example);
        if (extensionList.size()>0){
            //提取第一个元素
            return extensionList.get(0);
        }
        return null;
    }

}
